package com.jsmtr.rms.service.SystemLog;

import com.jsmtr.rms.dao.DaoService;
import com.jsmtr.rms.entity.SystemLog;
import com.jsmtr.rms.utils.StringTools;
import com.jsmtr.rms.utils.db.DbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: cyx
 * @description:
 * @date: Created in 2019/4/14 3:55
 * @modified by:
 */
@Service(value = "systemLogService")
public class SystemLogService implements ISystemLogService{

    @Autowired
    private DaoService daoService;

    public DaoService getDaoService() {
        return this.daoService;
    }

    public void setDaoService(DaoService daoService) {
        this.daoService = daoService;
    }

    @Override
    public void saveSystemLog(SystemLog systemLog) throws Exception {
        daoService.saveLog(systemLog);
    }

    @Override
    public List<SystemLog> getSystemList(SystemLog systemLog) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> getSystemListPage(int page, int perpage, Map params) throws Exception {
        StringBuffer sqlString= new StringBuffer(
                "select dur.cardno,\n" +
                        "       dur.name,\n" +
                        "       dur.adddate,\n" +
                        "       dur.proname,\n" +
                        "       flow.nickname,\n" +
                        "       flow.fname,\n" +
                        "       dur.jname,\n" +
                        "       lg.* \n" +
                        "  from system_log lg, Duserresume dur, Project_Info_Flow flow\n" +
                        " where lg.rid is not null\n" +
                        "   and lg.proid is not null\n" +
                        "   and lg.fid is not null\n" +
                        "   and lg.rid = dur.id\n" +
                        "   and lg.proid = flow.pid\n" +
                        "   and lg.fid = flow.id\n");
        StringBuffer countsql=new StringBuffer(
                "select count(1)\n" +
                "  from system_log lg, Duserresume dur, Project_Info_Flow flow\n" +
                " where lg.rid is not null\n" +
                "   and lg.proid is not null\n" +
                "   and lg.fid is not null\n" +
                "   and lg.rid = dur.id\n" +
                "   and lg.proid = flow.pid\n" +
                "   and lg.fid = flow.id\n" ) ;
        StringBuffer condition = new StringBuffer();
        Map<String, Object> map = new HashMap<String, Object>();
        if (params!=null&& StringTools.trimNotEmpty(params.get("cardno"))){
            condition.append("   and dur.cardno =:cardno \n");
            map.put("cardno",params.get("cardno"));
        }
        if (params!=null&& StringTools.trimNotEmpty(params.get("proid"))){
            condition.append("   and dur.proid =:proid \n");
            map.put("proid",params.get("proid"));
        }
        if (params!=null&& StringTools.trimNotEmpty(params.get("userid"))){
            condition.append("   and dur.id =:userid \n");
            map.put("userid",params.get("userid"));
        }
        if (params!=null&& StringTools.trimNotEmpty(params.get("proname"))){
            condition.append("   and dur.proname like :proname  \n");
            map.put("proname","%"+params.get("proname")+"%");
        }

        if (params!=null&& StringTools.trimNotEmpty(params.get("operatorcode"))){
            condition.append("   and lg.operatorcode =:operatorcode \n");
            map.put("operatorcode",params.get("operatorcode"));
        }
        sqlString.append(condition);
        countsql.append(condition);
        BigDecimal count = (BigDecimal) daoService.executeSQLQuery(countsql.toString(), map).get(0);
        double pageCount = Math.ceil(count.intValue() /  perpage);
        List list = daoService.executeSQLQueryMapPage(sqlString.toString(), map,perpage * (page - 1), perpage);
        Map rmap=new HashMap();
        rmap.put("pageCount", pageCount);
        rmap.put("count", count);
        rmap.put("list", list);
        return rmap;
    }

    @Override
    public Map<String, Object> getSystemLogStatisticsListPage(int page, int perpage, Map params) throws Exception {
        StringBuffer sqlString= new StringBuffer(
                "select * from VIEW_SYSOPLOG vslog where 1=1 \n");
        StringBuffer countsql=new StringBuffer(
                "select count(1)  from VIEW_SYSOPLOG vslog where 1=1 \n" ) ;
        StringBuffer condition = new StringBuffer();
        Map<String, Object> map = new HashMap<String, Object>();
        if (params!=null&& StringTools.trimNotEmpty(params.get("cardno"))){
            condition.append("   and vslog.cardno like :cardno \n");
            //map.put("cardno",params.get("cardno"));
            map.put("cardno","%"+params.get("cardno")+"%");
        }
        if (params!=null&& StringTools.trimNotEmpty(params.get("proid"))){
            condition.append("   and vslog.proid =:proid \n");
            map.put("proid",params.get("proid"));
        }
        if (params!=null&& StringTools.trimNotEmpty(params.get("userid"))){
            condition.append("   and vslog.id =:userid \n");
            map.put("userid",params.get("userid"));
        }
        if (params!=null&& StringTools.trimNotEmpty(params.get("proname"))){
            condition.append("   and vslog.proname like :proname  \n");
            map.put("proname","%"+params.get("proname")+"%");
        }

        if (params!=null&& StringTools.trimNotEmpty(params.get("operatorcode"))){
            condition.append("   and vslog.operatorcode =:operatorcode \n");
            map.put("operatorcode",params.get("operatorcode"));
        }


        if (params!=null&& StringTools.trimNotEmpty(params.get("operatordept"))){
            condition.append("   and vslog.operatordept like :operatordept \n");
            map.put("operatordept","%"+params.get("operatordept")+"%");
        }

        if (params!=null&& StringTools.trimNotEmpty(params.get("operatorname"))){
            condition.append("   and vslog.operatorname like :operatorname \n");
            map.put("operatorname","%"+params.get("operatorname")+"%");
        }

        sqlString.append(condition);
        countsql.append(condition);
        BigDecimal count = (BigDecimal) daoService.executeSQLQuery(countsql.toString(), map).get(0);
        double pageCount = Math.ceil(count.intValue() /  perpage);
        List list = daoService.executeSQLQueryMapPage(sqlString.toString(), map,perpage * (page - 1), perpage);
        Map rmap=new HashMap();
        rmap.put("pageCount", pageCount);
        rmap.put("count", count);
        rmap.put("list", list);
        return rmap;
    }


    @Override
    public List getDUserResumeLog(int page, int perpage, Map params) throws Exception {
        Map _map=new HashMap();
        String sqlString=
                "select dur.cardno,\n" +
                        "       dur.name,\n" +
                        "       dur.adddate,\n" +
                        "       dur.proname,\n" +
                        "       flow.nickname,\n" +
                        "       flow.fname,\n" +
                        "       dur.jname,\n" +
                        "       lg.* \n" +
                        "  from system_log lg, Duserresume dur, Project_Info_Flow flow\n" +
                        " where lg.rid is not null\n" +
                        "   and lg.proid is not null\n" +
                        "   and lg.fid is not null\n" +
                        "   and lg.rid = dur.id\n" +
                        "   and lg.proid = flow.pid\n" +
                        "   and lg.fid = flow.id\n" ;
                    if (params!=null&& StringTools.trimNotEmpty(params.get("cardno"))){
                        sqlString+= "   and dur.cardno =:cardno \n";
                        _map.put("cardno",params.get("cardno"));
                    }
                    if (params!=null&& StringTools.trimNotEmpty(params.get("proid"))){
                        sqlString+= "   and dur.proid =:proid \n";
                        _map.put("proid",params.get("proid"));
                    }
                    if (params!=null&& StringTools.trimNotEmpty(params.get("userid"))){
                        sqlString+= "   and dur.id =:userid \n";
                        _map.put("userid",params.get("userid"));
                    }
                    if (params!=null&& StringTools.trimNotEmpty(params.get("proname"))){
                        sqlString+= "   and dur.proname like :proname  \n";
                        _map.put("proname","%"+params.get("proname")+"%");
                    }

                    if (params!=null&& StringTools.trimNotEmpty(params.get("operatorcode"))){
                        sqlString+= "   and lg.operatorcode =:operatorcode \n";
                        _map.put("operatorcode",params.get("operatorcode"));
                    }
        List<Map<String, Object>> list = daoService.executeSQLQueryMapPage(sqlString, _map, perpage * (page - 1), perpage);
        return list;
    }

}
